package cn.edu.zut.sunday.news.domain;

import cn.edu.zut.sunday.base.domain.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.Objects;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.Hibernate;

@Entity
@Getter
@Setter
@Table(name = "tbl_newsCategory")
public class NewsCategory extends BaseEntity<Integer> {

    @Column(length = 64)
    private String category;//类别

    @Column
    private int count;//数量

    @JsonIgnore
    @OneToMany(mappedBy = "newsCategory", cascade = CascadeType.ALL)
    //级联保存、更新、删除、刷新;延迟加载。当删除用户，会级联删除该用户的所有文章
    //拥有mappedBy注解的实体类为关系被维护端
    //mappedBy="newsCategory"中的newsCategory是news中的author属性
    private Set<News> newsSet;

    @Override
    public boolean equals(Object o) {
        if (this == o) {return true;}
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(
          o)) {return false;}
        NewsCategory that = (NewsCategory) o;
        return id != null && Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}
